package com.onpositive.gamemaker.treeview;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.onpositive.gamemaker.editor.GameCreatorEditorPlugin;

public class DirViewerTooltipSupport extends ColumnViewerToolTipSupport {
	private final Tree tree;
	private Image image;

	private DirViewerTooltipSupport(Viewer viewer, int style, boolean manualActivation,
			TreeViewer invoke) {
		super(viewer, style, manualActivation);
		this.tree = invoke.getTree();
	}

	public DirViewerTooltipSupport(Tree ts, int style, boolean manualActivation) {
		super(ts, style, manualActivation);
		this.tree=ts;
	}

	protected boolean shouldCreateToolTip(Event event) {
		final TreeItem item = tree.getItem(
				new Point(event.x, event.y));
		if (item == null) {
			return false;
		}
		final Object data = item.getData();
		if (data instanceof IFile) {
			final IFile fs = (IFile) data;
			final String name2 = fs.getName();
			final boolean image = isImage(name2);
			return image;
		}
		else if (data instanceof File)
		{
			return isImage(((File) data).getName());
		}
		return false;
	}

	protected Composite createToolTipContentArea(Event event,
			Composite parent) {
		final Image currentImage = this.getImage(event);
		final Image bgImage = this.getBackgroundImage(event);
		final String text = this.getText(event);
		final Color fgColor = this.getForegroundColor(event);
		final Color bgColor = this.getBackgroundColor(event);
		final Font font = this.getFont(event);
		final FillLayout layout = (FillLayout) parent.getLayout();
		layout.marginWidth = 10;
		layout.marginHeight = 5;
		parent.setBackground(bgColor);
		final CLabel label = new CLabel(parent, this.getStyle(event));
		if (text != null) {
			label.setText(text);
		}

		if (currentImage != null) {
			label.setImage(currentImage);
		}

		if (fgColor != null) {
			label.setForeground(fgColor);
		}

		if (bgColor != null) {
			label.setBackground(bgColor);
		}

		if (bgImage != null) {
			label.setBackgroundImage(currentImage);
		}

		if (font != null) {
			label.setFont(font);
		}
		label.addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				currentImage.dispose();
				image = null;
			}

		});
		return label;
	}

	protected Image getImage(Event event) {
		final TreeItem item = tree.getItem(
				new Point(event.x, event.y));
		final Object data = item.getData();
		if (data instanceof IFile) {
			final IFile fs = (IFile) data;
			try {
				this.image = new Image(Display.getCurrent(), fs
						.getContents(true));
				return this.image;
			} catch (final CoreException e) {
				GameCreatorEditorPlugin.logError(e);
			}
		}
		else if (data instanceof File) {
			final File fs = (File) data;
			try {
				this.image = new Image(Display.getCurrent(), fs.getAbsolutePath());
				return this.image;
			} catch (RuntimeException e) {
				GameCreatorEditorPlugin.logError(e);
			}
		}
		return null;
	}

	protected String getText(Event event) {
		final TreeItem item = tree.getItem(
				new Point(event.x, event.y));
		final Object data = item.getData();
		if (data instanceof IFile) {
			final Image image2 = this.image;
			if (this.image != null) {
				final Rectangle bounds = image2.getBounds();
				return "(" + bounds.width + "," + bounds.height + ")";
			}
			return "";

		}
		return "";
	}
	
	public static boolean isImage(String name) {
		return name.endsWith(".gif") || name.endsWith(".jpg")
				|| name.endsWith(".png") || name.endsWith(".bmp")
				|| name.endsWith(".jpeg") || name.endsWith(".tiff");
	}
}